/*
 * @Author: yushuang
 * @Date: 2024-12-05 09:48:58
 * @LastEditors: yushuang
 * @LastEditTime: 2025-01-24 10:57:40
 * @Description:
 */
import { shallowRef, computed } from 'vue';
import { defineStore } from 'pinia';
import type { UserInfo } from '@/api/common/user';
import { getUserInfoApi } from '@/api/common/user';

export const useUserStore = defineStore('user', () => {
  const userInfo = shallowRef<UserInfo>();
  const avatar = computed(() => userInfo.value?.avatar);
  const nickname = computed(() => userInfo.value?.nickname ?? userInfo.value?.username);
  const roles = computed(() => userInfo.value?.roles);

  // 获取用户信息
  const getUserInfo = async () => {
    // 获取用户信息
    const { data } = await getUserInfoApi();
    userInfo.value = data;
  };

  return {
    userInfo,
    roles,
    getUserInfo,
    avatar,
    nickname
  };
});
